package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("PackingmethodController")
public class PackingmethodController extends CmiEmaController {

	@RequestMapping("/listPackingmethods")
	public ModelAndView listPackingmethods(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = packingmethodPaginationService.updatePagination(request, pagination);
		Set<Packingmethod> packingmethods = packingmethodPaginationService.getPaginationPackingmethods(pagination);

		mav.addObject("packingmethods", packingmethods);
		mav.addObject("entityName", "packingmethod");
		mav.addObject("pagination", pagination);
		mav.setViewName("packingmethod/listPackingmethods.jsp");
		return mav;
	}

	public String listPackingmethods() {
		return "redirect:/listPackingmethods";
	}

	@RequestMapping("/viewPackingmethod")
	public ModelAndView viewPackingmethod(@RequestParam Integer packingmethodId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("packingmethod", packingmethodDAO.findPackingmethodByPrimaryKey(packingmethodId));
		mav.setViewName("packingmethod/viewPackingmethod.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeletePackingmethods")
	public ModelAndView confirmDeletePackingmethods(
			@RequestParam(value = "deletePackingmethodsIds", required = true) Set<Integer> deletePackingmethodsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Packingmethod> deletePackingmethods = packingmethodDAO.findPackingmethodsByIds(deletePackingmethodsIds);
		Set<Packingmethod> referencedPackingmethods = packingmethodService
				.loadReferencePackingmethods(deletePackingmethods);
		boolean hasReferenced;
		if (referencedPackingmethods.size() == 0) {
			mav.addObject("deleteEntitys", deletePackingmethods);
			hasReferenced = false;
		} else {
			deletePackingmethods.removeAll(referencedPackingmethods);
			mav.addObject("referencedEntitys", referencedPackingmethods);
			mav.addObject("deleteEntitys", deletePackingmethods);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Packingmethod");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listPackingmethods");
		return mav;
	}

	@RequestMapping("/deletePackingmethod")
	public String deletePackingmethod(@RequestParam Integer packingmethodId) {
		Packingmethod packingmethod = packingmethodDAO.findPackingmethodByPrimaryKey(packingmethodId);
		packingmethodService.deletePackingmethod(packingmethod);
		return "forward:/listPackingmethods";
	}

	@RequestMapping("/confirmDeletePackingmethod")
	public ModelAndView confirmDeletePackingmethod(@RequestParam Integer packingmethodId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("packingmethod", packingmethodDAO.findPackingmethodByPrimaryKey(packingmethodId));

		mav.setViewName("packingmethod/deletePackingmethod.jsp");

		return mav;
	}

	@RequestMapping("/deletePackingmethods")
	public String deletePackingmethods(
			@RequestParam(value = "deletePackingmethodsId", required = true) Set<Integer> deletePackingmethodsId) {
		packingmethodService.deletePackingmethods(deletePackingmethodsId);
		return "forward:/listPackingmethods";
	}

	@RequestMapping("/editPackingmethod")
	public ModelAndView editPackingmethod(@RequestParam Integer packingmethodId) {

		ModelAndView mav = new ModelAndView();

		Packingmethod packingmethod = packingmethodDAO.findPackingmethodByPrimaryKey(packingmethodId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("packingmethod", packingmethod);

		// 把所有的引用packingmethod对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(packingmethodService.loadPackingmethodObjectReferencedSet(packingmethod));

		// 将编辑页面packingmethod添加到mav中
		mav.setViewName("packingmethod/editPackingmethod.jsp");
		return mav;
	}

	@RequestMapping("/newPackingmethod")
	public ModelAndView newPackingmethod() {
		ModelAndView mav = new ModelAndView();
		Packingmethod packingmethod = new Packingmethod();

		mav.addObject("packingmethod", packingmethod);

		// 把所有的引用packingmethod对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(packingmethodService.loadPackingmethodObjectReferencedSet(packingmethod));

		mav.addObject("newFlag", true);
		mav.setViewName("packingmethod/editPackingmethod.jsp");

		return mav;
	}

	@RequestMapping("/savePackingmethod")
	public ModelAndView savePackingmethod(@ModelAttribute Packingmethod packingmethod) {

		ModelAndView mav = new ModelAndView();

		Unit unitTemp = packingmethod.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Packingmethod existingPackingmethod = CmiHibernateProxyUtil.deproxy(packingmethodDAO
				.findPackingmethodByPhysicalPrimaryKey(packingmethod.getName(), packingmethod.getFee(), unit.getId()));

		packingmethod.setUnit(unit);
		// 新建记录
		if (packingmethod.getId() == null) {
			if (packingmethod.equals(existingPackingmethod)) {

				mav.addAllObjects(packingmethodService.loadPackingmethodClassReferencedSet(packingmethod));
				// mav = editContinue(mav, packingmethod);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("packingmethod/editPackingmethod.jsp");
			} else {
				packingmethodService.savePackingmethod(packingmethod);
				mav.setViewName("forward:/listPackingmethods");
			}
		}
		// 编辑记录
		else {
			Packingmethod originPackingmethod = CmiHibernateProxyUtil
					.deproxy(packingmethodDAO.findPackingmethodByPrimaryKey(packingmethod.getId()));
			if (!packingmethod.equals(originPackingmethod) && packingmethod.equals(existingPackingmethod)) {
				mav.addAllObjects(packingmethodService.loadPackingmethodClassReferencedSet(packingmethod));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("packingmethod/editPackingmethod.jsp");
			} else {
				packingmethodService.savePackingmethod(packingmethod);
				mav.setViewName("forward:/listPackingmethods");
			}
		}
		return mav;
	}

	@RequestMapping("/packingmethodController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
